<?php
	const PIC_PATH = './public/images/goods/';
	function __construct(){
		require './model/goods_model.php';
		$_GET['a']();
	}
	
	function index(){
		//条件查找
		if(!isset($_GET['tid'])) $_GET['tid'] = '-1';
		$condition = '';
		if(isset($_GET['tid']) && $_GET['tid']!='-1')  $while[] = " tid='$_GET[tid]' ";
		if(isset($_GET['gname'])&& $_GET['gname']!='' )$while[] = " gname LIKE '%$_GET[gname]%' ";
		if(isset($while)){
			$condition = ' WHERE '.implode(' AND ',$while);
		}
		//分页处理-------------------------------------------
		$setRows = 10;                     //每页条数
		$allRows = rowCount($condition);   //总条数
		$allPage = ceil($allRows/$setRows);//总页数
		$nowPage = empty($_GET['nowPage']) ? 1 : $_GET['nowPage'];//当前页
		$prevPage = $nowPage-1;
		$nextPage = $nowPage+1;
		
		if($prevPage < 1){
			$nowPage = 1;
			$prevPage = 1;
		}elseif($nextPage > $allPage){
			$nowPage = $allPage;
			$nextPage = $allPage;
		}

		$condition .=' LIMIT '.(($nowPage-1)*$setRows).','.$setRows;
		//-----------------------------------------------
		$ret = select($condition);       //取出数据
		//--------------------------------
		$types = getTypeInfo();
		if(!$types)$types=[];
		$arrTid = [0];
		$arrTname = ['根分类'];
		foreach($types as $v){
			$arrTid[] = $v['tid'];
			$arrTname[] = $v['tname'];
		}
		$arr = array_combine($arrTid,$arrTname);
		require './view/goods/index.php';
	}
	function edit(){
		$ret = select("WHERE gid = $_GET[id]")[0];
		$types = getTypeInfo();
		require './view/goods/edit.php';
	}
	
	function doedit(){
		//-------------------------------------------
		require './public/tools/fileUpload.php';
		require './public/tools/picZoom.php';
		$res = fileUpload(PIC_PATH,$_FILES['gpic'],100);
		if($res['flag']){
			$_POST['gpic'] = $res['picname'];
		}
		//-------------------------------------------
		if(update()){
			echo '修改成功!';
			echo "<meta http-equiv='refresh' content='0;./index.php?c=goods&a=index&nowPage=$_GET[nowPage]'>";
		}else{
			echo '修改失败!';
			echo "<meta http-equiv='refresh' content='2;./index.php?c=goods&a=index&nowPage=$_GET[nowPage]'>";
		}
		
	}
	function status(){
		$_POST['status'] = $_GET['status'];
		if(update()){
			echo '修改成功!';
			echo "<meta http-equiv='refresh' content='0;./index.php?c=goods&a=index&nowPage=$_GET[nowPage]'>";
		}else{
			echo '修改失败!';
			echo "<meta http-equiv='refresh' content='2;./index.php?c=goods&a=index&nowPage=$_GET[nowPage]'>";
		}
		
	}
	function add(){
		$types = getTypeInfo();
		if(!$types)$types=[];
		require './view/goods/add.php';
	}
	function doadd(){
		$setRows = 10;           //每页条数
		$allRows = rowCount()+1;   //总条数
		$allPage = ceil($allRows/$setRows);//总页数
		//-------------------------------------------
		require './public/tools/fileUpload.php';
		require './public/tools/picZoom.php';
		$res = fileUpload(PIC_PATH,$_FILES['gpic'],100);
		if($res['flag']){
			$_POST['gpic'] = $res['picname'];
		}
		//-------------------------------------------
		if(insert()){
			echo '添加成功!';
			echo "<meta http-equiv='refresh' content='0;./index.php?c=goods&a=index&nowPage=$allPage'>";
		}else{
			echo '添加失败!';
			echo "<meta http-equiv='refresh' content='2;./index.php?c=goods&a=index&nowPage=$allPage'>";
		}
		
	}
	function del(){
		if($_GET['pic'] != 'normal.jpg'){
			@unlink(PIC_PATH.$_GET['pic']);
			@unlink(PIC_PATH.'sm_'.$_GET['pic']);
		}
		if(delete()){
			echo '删除成功!';
			echo "<meta http-equiv='refresh' content='0;./index.php?c=goods&a=index&nowPage=$_GET[nowPage]'>";
		}else{
			echo '删除失败!';
			echo "<meta http-equiv='refresh' content='2;./index.php?c=goods&a=index&nowPage=$_GET[nowPage]'>";
		}
		
	}